import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

public class HangmanGame {
    public static void main(String[] args) {
        HangmanGame hg = new HangmanGame();
        hg.showMenu();
    }

    public void showMenu() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("\n------ Menu ------");
            System.out.println("1. Play");
            System.out.println("2. Instructions");
            System.out.println("3. Exit");
            System.out.print("Choose your option: ");
            int option = sc.nextInt();
            sc.nextLine(); // Consume the newline

            switch (option) {
                case 1:
                    playGame();
                    break;
                case 2:
                    readInstructions();
                    break;
                case 3:
                    System.out.println("Exiting...");
                    return;
                default:
                    System.out.println("Invalid option! Please try again.");
            }
        }
    }

    public void playGame() {
        String[] words = {"japan", "qatar", "syria", "mongolia", "bahrain", "india", "china"};
        Random random = new Random();
        Scanner input = new Scanner(System.in);
        boolean playAgain;

        do {
            String targetWord = words[random.nextInt(words.length)];
            int length = targetWord.length();
            char[] currentGuess = new char[length];
            Arrays.fill(currentGuess, '_');
            StringBuilder wrongGuesses = new StringBuilder();

            System.out.println("\nNew Game! Guess the word:");
            System.out.println("Pattern: " + new String(currentGuess));

            while (true) {
                System.out.print("\nEnter a letter: ");
                String inputStr = input.nextLine().toLowerCase();

                // Validate input
                if (inputStr.length() != 1 || !Character.isLetter(inputStr.charAt(0))) {
                    System.out.println("Please enter a single alphabetical character.");
                    continue;
                }

                char guess = inputStr.charAt(0);
                boolean correctGuess = false;

                // Check if the guessed letter is in the word
                for (int i = 0; i < length; i++) {
                    if (targetWord.charAt(i) == guess) {
                        currentGuess[i] = guess;
                        correctGuess = true;
                    }
                }

                if (!correctGuess) {
                    wrongGuesses.append(guess).append(" ");
                    System.out.println("Missed letters: " + wrongGuesses);
                }

                // Display current progress
                System.out.println("Current word: " + new String(currentGuess));

                // Check if the word is fully guessed
                if (targetWord.equals(new String(currentGuess))) {
                    System.out.println("\n------ Congratulations! You won! ------");
                    break;
                }
            }

            System.out.print("\nPlay again? (y/n): ");
            String choice = input.nextLine();
            playAgain = choice.equalsIgnoreCase("y");
        } while (playAgain);
    }

    public void readInstructions() {
        System.out.println("\n===== Instructions =====");
        System.out.println("1. The computer randomly selects a word.");
        System.out.println("2. Guess letters one at a time.");
        System.out.println("3. Correct guesses reveal the letter(s) in the word.");
        System.out.println("4. Incorrect guesses are recorded as misses.");
        System.out.println("5. Win by guessing all letters in the word before too many misses.");
    }
}